package com.lindroid.androiduilearning.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * @author Lin
 * @date 2018/3/10
 * @function 自定义了字体的TextView
 */
@SuppressLint("AppCompatCustomView")
public class FontTextView extends TextView {

    private final String CUSTOM_FONT = "fonts/precious.ttf";

    public FontTextView(Context context) {
        this(context, null);
    }

    public FontTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FontTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        replaceCustomFont();
    }

    private void replaceCustomFont() {
        Typeface typeface = getTypeface();
        int style = Typeface.NORMAL;
        if (typeface != null) {
            style = typeface.getStyle();
        }
        Typeface newTypeface = Typeface.createFromAsset(getContext().getAssets(), CUSTOM_FONT);
        setTypeface(newTypeface, style);
    }
}
